const allServices = require('../../controller/mysql')
const jwt = require('jsonwebtoken');
const secret = 'mysecretkey';
const { timeHanle, getTime } = require('../../util/oderNo')
const user = {
    login: (req, res) => {
        let ip = req.connection.remoteAddress.substr(7);
        // const userAgent = req.headers['user-agent'];
        // console.log(ip, userAgent);
        const { username, password } = req.body
        const logSql = `insert into log (username, user_ip, isSuccess, login_time) 
        values (?, ?, ?, ?)`
        const sql = `select * from user where username=?`
        allServices.query(sql, [username]).then(result => {
            if (result.length) {
                if (result[0].username === username && result[0].passward == password) {
                    // 用户验证成功，生成Token
                    const payload = { user_id: result[0].user_id };
                    const options = { expiresIn: '1h' };
                    const token = jwt.sign(payload, secret, options);
                    res.send({
                        code: 200,
                        msg: '登录成功',
                        token: token,
                        user_id: result[0].user_id
                    })
                    const sql = `update user set token = ? where user_id = ?`
                    allServices.query(sql, [token, result[0].user_id])
                    allServices.query(logSql, [username, ip, '登录成功', getTime()])
                }
                else {
                    allServices.query(logSql, [username, ip, '登录失败', getTime()])
                    res.send({
                        code: 404,
                        msg: '账号或密码错误'
                    })
                }
            }
            else {
                allServices.query(logSql, [username, ip, '登录失败', getTime()])
                res.send({
                    code: 404,
                    msg: '账号或密码错误'
                })
            }
        })
    },
    register: (req, res) => {
        const { username, password, mobile } = req.body
        const sql = `insert into user (username, passward, tel) values (?, ?, ?)`
        const sql1 = `select * from user where username=?`
        allServices.query(sql1, [username]).then(res2 => {
            if (res2.length) {
                res.send({
                    code: 400,
                    msg: '用户名重复'
                })
            }
            else {
                allServices.query(sql, [username, password, mobile]).then(res1 => {
                    // console.log(res1.affectedRows);
                    if (res1.affectedRows) {
                        res.send({
                            code: 200,
                            msg: '注册成功'
                        })
                    }
                })
            }
        })
    },
    log: (req, res) => {
        const sql = 'select * from log'
        allServices.query(sql).then(result => {
            result.forEach(item => {
                item.login_time = timeHanle(item.login_time)
            })
            res.send({
                code: 200,
                msg: 'success',
                res: result
            })
        })
    }
}
module.exports = user